layui.config({  // common.js是配置layui扩展模块的目录，每个页面都需要引入
    version: '318',   // 更新组件缓存，设为true不缓存，也可以设一个固定值
    base: getProjectUrl() + 'assets/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: 'step-lay/step',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    echarts: 'echarts/echarts',
    echartsTheme: 'echarts/echartsTheme',
    numinput: 'numinput/numinput',
    introJs: 'introJs/introJs',
    tagsInput: 'tagsInput/tagsInput',
    cascader: 'cascader/cascader',
    zTree: 'zTree/zTree',
    soulTable: 'soulTable/soulTable',
    tableChild: 'soulTable/tableChild',
    tableMerge: 'soulTable/tableMerge',
    tableFilter: 'soulTable/tableFilter',
    excel: 'soulTable/excel',
}).use(['layer', 'admin', 'element'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var admin = layui.admin;
    var element = layui.element;

});

//扩展图标(Font class/Symbol)
document.write('<link rel="stylesheet" href="' + getProjectUrl() + 'assets/font/iconfont.css?v=1.3.9"/>')
var symbolScript = document.createElement("script");
symbolScript.src = getProjectUrl() + 'assets/font/iconfont.js?v=1.3.9';
document.head.appendChild(symbolScript);

/** 获取当前项目的根路径，通过获取layui.js全路径截取assets之前的地址 */
function getProjectUrl() {
    var layuiDir = layui.cache.dir;
    if (!layuiDir) {
        var js = document.scripts, last = js.length - 1, src;
        for (var i = last; i > 0; i--) {
            if (js[i].readyState === 'interactive') {
                src = js[i].src;
                break;
            }
        }
        var jsPath = src || js[last].src;
        layuiDir = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
    }
    return layuiDir.substring(0, layuiDir.indexOf('assets'));
}

//文件上传接口
function uploadImgUrl() {
    return "https://fileupload.danetvn.com/api/attachment/upload?type=huabeifile";
}

//文件访问域名
function imgFileDomain() {
    return "https://huabeifile.corntoast.cn";
}

//预览图片
function showImg(imgUrl) {
    if (imgUrl) {
        var img = '<img src="' + imgUrl + '">';
        var setting = {
            type: 1,
            title: false,
            closeBtn: 0,
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            shade: 0.6, //遮罩透明度
            content: img
        }
        var windowH = layui.jquery(window).height();
        var windowW = layui.jquery(window).width();
        getImageWidth(imgUrl, function (w, h) {
            // 调整图片大小
            if (w > windowW || h > windowH) {
                if (w > windowW && h > windowH) {
                    w = windowW - 100;
                    h = windowH - 100;
                    setting.area = [w + "px", h + "px"];
                } else if (w > windowW) {
                    setting.area = [windowW + "px", windowW * 0.5 / w * h + "px"];
                } else {
                    setting.area = [w + "px", windowH + "px"];
                }
            } else {
                setting.area = [w + "px", h + "px"];
            }
            // 设置layer
            layer.open(setting);
        });
    }
}

function getImageWidth(url, callback) {
    var img = new Image();
    img.src = url;
    // 如果图片被缓存，则直接返回缓存数据
    if (img.complete) {
        callback(img.width, img.height);
    } else {
        // 完全加载完毕的事件
        img.onload = function () {
            callback(img.width, img.height);
        }
    }
}

/**
 * POST请求下载Excel文件
 * @param url
 * @param data
 */
function openPostWindow(url, data) {
    var tempForm = document.createElement("form");
    tempForm.id = "tempForm1";
    tempForm.method = "post";
    tempForm.action = url;
    tempForm.target = "_blank"; //打开新页面
    jQuery.each(data, function (i, obj) {
        var hideInput1 = document.createElement("input");
        hideInput1.type = "hidden";
        hideInput1.name = i; //后台要接受这个参数来取值
        hideInput1.value = obj; //后台实际取到的值
        tempForm.appendChild(hideInput1);
    });
    document.body.appendChild(tempForm);
    tempForm.submit();
    document.body.removeChild(tempForm);
}

/**
 * 判断字符串是否是json格式
 * @param str
 * @returns {boolean}
 */
function isJSON(str) {
    if (typeof str == 'string') {
        try {
            var obj=JSON.parse(str);
            if(typeof obj == 'object' && obj ){
                return true;
            }else{
                return false;
            }
        } catch(e) {
            return false;
        }
    }
}

/**
 * 判断字符串是否为空
 * @param str
 * @returns {boolean}
 */
function isEmpty(value) {
    if (value == null || this.trim(value) == "" || value == undefined || value == "undefined") {
        return true;
    }
    return false;
}

/**
 * 判断一个字符串是否为非空串
 * @param value
 * @returns {boolean}
 */
function isNotEmpty(value) {
    return !isEmpty(value);
}

/**
 * 空格截取
 * @param value
 * @returns {string}
 */
function trim(value){
    if (value == null) {
        return "";
    }
    return value.toString().replace(/(^\s*)|(\s*$)|\r|\n/g, "");
}

/**
 * 比较两个字符串（大小写敏感）
 * @param str
 * @param that
 * @returns {boolean}
 */
function equals(str, that){
    return str == that;
}

/**
 * 比较两个字符串（大小写不敏感）
 * @param str
 * @param that
 * @returns {boolean}
 */
function equalsIgnoreCase(str, that) {
    return String(str).toUpperCase() === String(that).toUpperCase();
}


/**
 * 将字符串按指定字符分割
 * @param str
 * @param sep
 * @param maxLen
 * @returns {null|string[]}
 */
function split(str, sep, maxLen) {
    if (isEmpty(str)) {
        return null;
    }
    var value = String(str).split(sep);
    return maxLen ? value.slice(0, maxLen - 1) : value;
}

/**
 * 回显字典
 * @param dictDataArray 字典列表
 * @param value 字典值
 * @returns {string} 字典名称
 */
function getDictName(dictDataArray, value) {
    if (isEmpty(dictDataArray) || isEmpty(value)){
        return '';
    }
    var actions = [];
    dictDataArray.forEach(function (val, index, arr) {
        if (val.dictValue == ('' + value)) {
            actions.push(val.dictName);
            return false;
        }
    });

    if (actions.length === 0) {
        actions.push(value)
    }
    return actions.join('');
}

function test() {
    console.log(new Date())
}
